import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class ComponentInWindow extends JFrame {JTextField text;
    JCheckBox checkBox1, checkBox2, checkBox3;
    JRadioButton radio1, radio2;
    ButtonGroup group;
    JComboBox<String> comboBox;
    JTextArea area;
    JButton confirmButton;  // Add the new button

    public ComponentInWindow() {
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);  // Set window size
    }

    void init() {
        setLayout(new BorderLayout());  // Use BorderLayout for the main frame

        // Panel for other components, placed in the center
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());  // Use FlowLayout for center components

        // Add components to the centerPanel
        centerPanel.add(new JLabel("姓名"));
        text = new JTextField(20);  // Initialize text field with a column size
        centerPanel.add(text);

        centerPanel.add(new JLabel("爱好："));
        checkBox1 = new JCheckBox("喜欢音乐");
        checkBox2 = new JCheckBox("喜欢旅游");
        checkBox3 = new JCheckBox("喜欢篮球");
        centerPanel.add(checkBox1);
        centerPanel.add(checkBox2);
        centerPanel.add(checkBox3);

        centerPanel.add(new JLabel("性别"));
        group = new ButtonGroup();
        radio1 = new JRadioButton("男");
        radio2 = new JRadioButton("女");
        group.add(radio1);
        group.add(radio2);
        centerPanel.add(radio1);
        centerPanel.add(radio2);

        centerPanel.add(new JLabel("社团:"));
        comboBox = new JComboBox<>();
        comboBox.addItem("音乐天地");
        comboBox.addItem("武术天地");
        comboBox.addItem("象棋乐园");
        centerPanel.add(comboBox);

        centerPanel.add(new JLabel("其他介绍:"));
        area = new JTextArea(6, 12);
        centerPanel.add(new JScrollPane(area));

        add(centerPanel, BorderLayout.CENTER);  // Add the center panel to the center region

        // Button placed at the bottom
        confirmButton = new JButton("确定");
        add(confirmButton, BorderLayout.SOUTH);  // This will be placed at the bottom

        // Button click action to save student information
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveStudentInfo();
            }
        });
    }

    // Method to save student info to a file and open the file
    private void saveStudentInfo() {
        // Get the user input information
        String name = text.getText();
        String gender = radio1.isSelected() ? "男" : (radio2.isSelected() ? "女" : "未选择");
        String hobbies = "";
        if (checkBox1.isSelected()) hobbies += "喜欢音乐 ";
        if (checkBox2.isSelected()) hobbies += "喜欢旅游 ";
        if (checkBox3.isSelected()) hobbies += "喜欢篮球 ";
        String club = (String) comboBox.getSelectedItem();
        String introduction = area.getText();

        // Construct student info string
        String studentInfo = String.format("姓名: %s, 性别: %s, 爱好: %s, 社团: %s, 其他介绍: %s%n",
                name, gender, hobbies, club, introduction);

        // Save to file
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("students.txt", true))) {
            writer.write(studentInfo);  // Append to the file
            JOptionPane.showMessageDialog(this, "学生信息已保存！");

            // After saving, open the file using the default text editor
            openFile("students.txt");

        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "保存学生信息失败：" + ex.getMessage());
        }
    }

    // Method to open the file using the default text editor
    private void openFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.exists()) {
                // Use Desktop to open the file with the default editor
                Desktop.getDesktop().open(file);
            } else {
                JOptionPane.showMessageDialog(this, "文件不存在！");
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "无法打开文件：" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new ComponentInWindow();  // Create and display the window
    }
}

